<template>
    <div class="SnackUpload">
        <h3>头像上传</h3>
        <div id="headImg">
            <form id="myform">
                <input type="hidden" id="urlpath" name="urlpath" value="${pageContext.request.contextPath}">
                <input type="hidden" id="imgData" name="imgData">
                <input type="file" id="file" name="file" @change="showImg()" ref="file">点这里上传图片<br><br>
            </form>
            <div class="imgS" :class="{hidden:jude}">
            <div ref="imgFather" class="imgFather">
            </div>
                <div class="smallFather">
                    <div class="smallFather-1">
            <div class="small">
            </div>
                        <p>200 x 200</p>
                    </div>
                    <div class="smallFather-2">
            <div class="small small-1">
            </div>
                        <p>100 x 100</p>
                    </div>
                </div>
            </div>
            <button @click="uploadImg()" :class="{hidden:jude}">上传</button>
        </div>
    </div>
</template>

<script>
    import Cropper from '../../public/cropperjs/dist/cropper.min.js'
    import Info from "./Info";
    export default {
        name: "SnackUpload",
        components: {Info},
        data(){
            return {
                imageSave:{},
                uploadDate:"",
                imgURL:"",
                jude:true,
                format:'',
                price:'',
                TradeName:''

            }
        },
        methods:{
            showImg(){
                this.jude=false
              const file = this.$refs.file.files[0]
              var reader = new FileReader();
              reader.readAsDataURL(file)
                var _this = this;

              reader.onload=function (e) {
                  //获取图片的base64字符串
                  var imgBase64Data = e.target.result;

                  imgBase64Data.replace(/(jpeg)|(jpg)|(png)|(gif)/,function (match,param,offset,string) {
                                    _this.format=match
                  })
                  _this.$refs.imgFather.innerHTML = "<img src='"+imgBase64Data+"' id='imgT1'>"
                  var image = document.getElementById('imgT1');
                  var cropper = new Cropper(image,{
                      aspectRatio:15/15,
                      crop(event){
                      },
                      preview:".small",
                      movable:false,
                      zoomale:false,
                      mouseWheelZoom: false,
                      scalable:false,
                      zoomOnWheel:false,
                      background:false,
                      viewMode:1
                  });
                  _this.imageSave = cropper;
              }
            },
            uploadImg(){
              var cad = this.imageSave.getCroppedCanvas();
              var format = 'image/'+this.format
              var Base64URL = cad.toDataURL(format);
              var request = this.myCode();
              var config={
                  method:'post',
                  url:'/BlogPort',
                  data:{
                      imgUrl:Base64URL,
                      api_token:localStorage.getItem('API')
                  }
              }
              request(config).then((res)=>{
                   if(res.data.status == 404){
                       return this.$router.push({name:'Login'})
                   }
              })
            }
        },
        created() {
            if(!localStorage.getItem('API')){
                return this.$router.push({name:'Login'})
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import "../../public/cropperjs/dist/cropper.min.css";
    img {
        display: block;

        /* This rule is very important, please don't ignore this */
        max-width: 100%;
        max-height: 100%;
    }
    #myform {
        margin-top: 30px;
        padding: 4px 10px;
        height: 20px;
        line-height: 20px;
        position: relative;
        cursor: pointer;
        color: #888;
        background: #fafafa;
        border: 1px solid #ddd;
        border-radius: 4px;
        overflow: hidden;
        display: inline-block;
        *display: inline;
        *zoom: 1
    }

    #myform  input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
        filter: alpha(opacity=0);
        cursor: pointer
    }

    #myform:hover {
        color: #444;
        background: #eee;
        border-color: #ccc;
        text-decoration: none
    }
   .SnackUpload{
       font-family: "Apple SD Gothic Neo", "Malgun Gothic", "Nanum Gothic", Dotum, sans-serif;
       padding-top: 20px;
       height: 100vh;
       width: 100%;
      margin-bottom: 30px;
       h3{
           text-align: center;
       }

   }
   #headImg{
       width: 800px;
       margin: 0 auto;
       text-align: center;
   }
   .smallFather-1{
       width: 200px;
       height: 200px;
   }
     .small{
         width: 100%;
         height: 100%;
         overflow: hidden;
         position: relative;
     }
    .small img{
        height: 100%;
        width: 100%;
    }
    .imgS{
        margin-top: 30px;
        display: flex;
        justify-content: space-between;
        .smallFather{
            display: flex;
            flex-direction: column;
            justify-content: space-around;
        }
    }
    .imgFather{
        width: 400px;
        height: 400px;
         img{
        width: 100%;
             height: 100%;
      }

    }
    .smallFather-2{
        height: 100px;
        width: 100px;
    }
    .small-1{
        width: 100px;
        height: 100px;
        overflow: hidden;
    }
    .hidden{
        display: none;
    }
    button{
        width: 30%;
        height: 30px;
        border-radius: 5px;
        border:none;
        background-color: #00bf00;
        color: white;
        margin-top: 50px;
    }
    .input input{
        width: 250px;
        height: 35px;
        margin-top: 30px;
        border-radius: 8px;
        padding-left: 20px;
        border: 1px solid slategray;
        outline: none;
    }

    @media screen and (max-width: 440px) {
      #headImg{
        width: 360px;
      }
      .imgS{
        flex-direction: column;
      }
      .imgS .smallFather{
        margin-top: 30px;
        flex-direction: row;
        margin-bottom: 30px;
      }
      .imgFather{
        width: 360px;
        height: 360px;
      }
    }
</style>